extends Node

# 自动售卖管理器
# 负责处理装备的自动售卖逻辑

# 自动售卖设置
var auto_sell_settings = {
	"enabled": false,
	"max_level": 1,
	"qualities": {
		"COMMON": false,
		"UNCOMMON": false,
		"RARE": false,
		"EPIC": false,
		"LEGENDARY": false
	}
}

# 设置文件路径
const SETTINGS_FILE_PATH = "user://auto_sell_settings.json"

func _ready():
	"""初始化自动售卖管理器"""
	load_settings()
	print("AutoSellManager 已初始化")

func load_settings():
	"""从文件加载设置"""
	if FileAccess.file_exists(SETTINGS_FILE_PATH):
		var file = FileAccess.open(SETTINGS_FILE_PATH, FileAccess.READ)
		if file:
			var json_string = file.get_as_text()
			file.close()
			
			var json = JSON.new()
			var parse_result = json.parse(json_string)
			if parse_result == OK:
				auto_sell_settings = json.data
				print("AutoSellManager: 设置已加载")
			else:
				print("AutoSellManager: 设置文件解析失败")
		else:
			print("AutoSellManager: 无法打开设置文件")
	else:
		print("AutoSellManager: 设置文件不存在，使用默认设置")

func update_settings(new_settings):
	"""更新设置"""
	auto_sell_settings = new_settings
	print("AutoSellManager: 设置已更新")

func should_auto_sell_item(item_data):
	"""检查物品是否应该自动售卖"""
	# 如果自动售卖未启用，返回false
	if not auto_sell_settings.enabled:
		return false
	
	# 检查物品类型（仅限装备）
	# 支持字典格式和EquipmentData对象
	var has_slot = false
	if typeof(item_data) == TYPE_DICTIONARY:
		has_slot = item_data.has("slot")
	else:
		# 假设是EquipmentData对象
		has_slot = item_data.has_method("get") or "slot" in item_data
	
	if not has_slot:
		return false
	
	# 检查等级条件
	var item_level = 1
	if typeof(item_data) == TYPE_DICTIONARY:
		item_level = item_data.get("level_requirement", 1)
	else:
		# EquipmentData对象
		item_level = item_data.level_requirement if "level_requirement" in item_data else 1
	
	if item_level > auto_sell_settings.max_level:
		return false
	
	# 检查品质条件
	var item_rarity = "COMMON"
	if typeof(item_data) == TYPE_DICTIONARY:
		item_rarity = item_data.get("rarity", "COMMON")
	else:
		# EquipmentData对象，需要转换枚举为字符串
		if "rarity" in item_data:
			# 将ItemRarity枚举转换为字符串
			match item_data.rarity:
				ItemManager.ItemRarity.COMMON:
					item_rarity = "COMMON"
				ItemManager.ItemRarity.UNCOMMON:
					item_rarity = "UNCOMMON"
				ItemManager.ItemRarity.RARE:
					item_rarity = "RARE"
				ItemManager.ItemRarity.EPIC:
					item_rarity = "EPIC"
				ItemManager.ItemRarity.LEGENDARY:
					item_rarity = "LEGENDARY"
	
	if not auto_sell_settings.qualities.has(item_rarity):
		return false
	
	return auto_sell_settings.qualities[item_rarity]

func auto_sell_item(item_data):
	"""自动售卖物品"""
	if not should_auto_sell_item(item_data):
		return false
	
	# 计算售价（装备全价）
	var sell_price = 1
	if typeof(item_data) == TYPE_DICTIONARY:
		sell_price = item_data.get("value", 1)
	else:
		# EquipmentData对象
		sell_price = item_data.value if "value" in item_data else 1
	
	# 获取物品信息用于日志
	var item_name = "未知物品"
	var item_level = 1
	var item_rarity_str = "COMMON"
	
	if typeof(item_data) == TYPE_DICTIONARY:
		item_name = item_data.get("name", "未知物品")
		item_level = item_data.get("level_requirement", 1)
		item_rarity_str = item_data.get("rarity", "COMMON")
	else:
		# EquipmentData对象
		item_name = item_data.name if "name" in item_data else "未知物品"
		item_level = item_data.level_requirement if "level_requirement" in item_data else 1
		# 转换枚举为字符串
		if "rarity" in item_data:
			match item_data.rarity:
				ItemManager.ItemRarity.COMMON:
					item_rarity_str = "COMMON"
				ItemManager.ItemRarity.UNCOMMON:
					item_rarity_str = "UNCOMMON"
				ItemManager.ItemRarity.RARE:
					item_rarity_str = "RARE"
				ItemManager.ItemRarity.EPIC:
					item_rarity_str = "EPIC"
				ItemManager.ItemRarity.LEGENDARY:
					item_rarity_str = "LEGENDARY"
	
	# 添加金币到玩家
	if PlayerStatsManager:
		PlayerStatsManager.add_gold(sell_price)
		print("自动售卖: %s (等级%d, %s) - 获得 %d 金币" % [
			item_name,
			item_level,
			item_rarity_str,
			sell_price
		])
		return true
	
	return false

func get_settings():
	"""获取当前设置"""
	return auto_sell_settings

func is_enabled():
	"""检查自动售卖是否启用"""
	return auto_sell_settings.enabled
